using MediatR;
using SumerCoreDevOps.Application.NodeRegistration.Commands;
using SumerCoreDevOps.Application.NodeRegistration.Queries;
using SumerCoreDevOps.Domain.Entities;
using SumerCoreDevOps.Infrastructure.NodeRegistration;

namespace SumerCoreDevOps.Application.NodeRegistration.Handlers;

public class GenerateTokenCommandHandler : IRequestHandler<GenerateTokenCommand, NodeJoinToken>
{
    private readonly NodeJoinTokenManager _tokenManager;

    public GenerateTokenCommandHandler(NodeJoinTokenManager tokenManager)
    {
        _tokenManager = tokenManager;
    }

    public async Task<NodeJoinToken> Handle(GenerateTokenCommand request, CancellationToken cancellationToken)
    {
        return await _tokenManager.GenerateTokenAsync(
            request.Name,
            request.Description,
            request.ExpiresInHours,
            request.MaxUsageCount,
            request.CreatedBy
        );
    }
}

public class RevokeTokenCommandHandler : IRequestHandler<RevokeTokenCommand>
{
    private readonly NodeJoinTokenManager _tokenManager;

    public RevokeTokenCommandHandler(NodeJoinTokenManager tokenManager)
    {
        _tokenManager = tokenManager;
    }

    public async Task Handle(RevokeTokenCommand request, CancellationToken cancellationToken)
    {
        await _tokenManager.RevokeTokenAsync(request.TokenId, request.Reason, request.RevokedBy);
    }
}

public class DeleteTokenCommandHandler : IRequestHandler<DeleteTokenCommand>
{
    private readonly NodeJoinTokenManager _tokenManager;

    public DeleteTokenCommandHandler(NodeJoinTokenManager tokenManager)
    {
        _tokenManager = tokenManager;
    }

    public async Task Handle(DeleteTokenCommand request, CancellationToken cancellationToken)
    {
        await _tokenManager.DeleteTokenAsync(request.TokenId);
    }
}

public class GetAllTokensQueryHandler : IRequestHandler<GetAllTokensQuery, List<NodeJoinToken>>
{
    private readonly NodeJoinTokenManager _tokenManager;

    public GetAllTokensQueryHandler(NodeJoinTokenManager tokenManager)
    {
        _tokenManager = tokenManager;
    }

    public async Task<List<NodeJoinToken>> Handle(GetAllTokensQuery request, CancellationToken cancellationToken)
    {
        return await _tokenManager.GetAllTokensAsync(request.IncludeRevoked);
    }
}

public class GetTokenQueryHandler : IRequestHandler<GetTokenQuery, NodeJoinToken?>
{
    private readonly NodeJoinTokenManager _tokenManager;

    public GetTokenQueryHandler(NodeJoinTokenManager tokenManager)
    {
        _tokenManager = tokenManager;
    }

    public async Task<NodeJoinToken?> Handle(GetTokenQuery request, CancellationToken cancellationToken)
    {
        return await _tokenManager.GetTokenAsync(request.TokenId);
    }
}

public class ValidateTokenQueryHandler : IRequestHandler<ValidateTokenQuery, (bool isValid, string? errorMessage)>
{
    private readonly NodeJoinTokenManager _tokenManager;

    public ValidateTokenQueryHandler(NodeJoinTokenManager tokenManager)
    {
        _tokenManager = tokenManager;
    }

    public async Task<(bool isValid, string? errorMessage)> Handle(ValidateTokenQuery request, CancellationToken cancellationToken)
    {
        var (isValid, errorMessage, _) = await _tokenManager.ValidateTokenAsync(request.Token);
        return (isValid, errorMessage);
    }
}
